package com.qf;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.Properties;

/**
 * Created by luoxinmin on 2020/6/8.
 */
public class MyFactory {
    private Properties pro = new Properties();

     {
        try {
            pro.load(MyFactory.class.getClassLoader().getResourceAsStream("info.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Object getBean(String beanName) {
        String bean = pro.getProperty(beanName);//获得全类名称
        try {
            if (bean != null) {
                Class<?> clazz = Class.forName(bean);//反射：类对象
                Constructor<?> c = clazz.getDeclaredConstructor();
                Object obj = c.newInstance();//当前类的实例
                return obj;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
